import java.math.BigDecimal;
import java.text.ParseException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.zone.ZoneRules;

public class Classfy {
    public Classfy() {
    }

    public long calculateTimeSpan(LocalDateTime startTime, LocalDateTime endTime) throws ParseException {
        long flagTime=0;
        //时区转换
        ZonedDateTime begin = ZonedDateTime.of(startTime, ZoneId.of("America/Chicago"));
        ZonedDateTime end = ZonedDateTime.of(endTime, ZoneId.of("America/Chicago"));
        ZoneId zoneId = begin.getZone();
        ZoneRules rules = zoneId.getRules();
        //判断是否是夏令时
        boolean beginTiming = rules.isDaylightSavings(begin.toInstant());
        boolean endTiming = rules.isDaylightSavings(end.toInstant());
        //因为java内部在对有时区的时间相减时会自动考虑时区问题，这里把时间转为没有时区划分的本地时间
        ZonedDateTime startTimeNow = ZonedDateTime.of(startTime,ZoneId.systemDefault());
        ZonedDateTime endTimeNow = ZonedDateTime.of(endTime,ZoneId.systemDefault());
        Duration duration = Duration.between(startTimeNow,endTimeNow);
        long seconds = duration.getSeconds();
        /*a、通话开始时间和结束时间都在夏令时,或者都在冬令时*/
        if (beginTiming == endTiming) {
            flagTime = seconds;
        }
        /*b、通话从夏令时跨越到冬令时*/
        if (beginTiming && !endTiming) {
            flagTime = seconds + 60 * 60;
        }
        /*c、通话从冬令时跨越到夏令时*/
        if (!beginTiming && endTiming) {
            flagTime = seconds -  60 * 60;
        }
        long dec = flagTime;
        if (dec%60 != 0){
            flagTime = (dec-dec%60)/60 + 1;
        }else {
            flagTime = dec/60;
        }
        return flagTime;
    }
    public BigDecimal calculateFee(long timeLength) {
        BigDecimal price;
        /*通话时长小于20分钟*/
        if (timeLength <= 20.0) {
            double newPrice = timeLength * 0.05;
            price = new BigDecimal(Double.toString(newPrice));
        }
        /*通话时长多余20分钟但小于30小时*/
        else if (timeLength >= 20 && timeLength <= 1800) {
            timeLength = timeLength - 20;
            double newPrice = 1 + timeLength * 0.1;
            price = new BigDecimal(Double.toString(newPrice));
        } /*通话多于30小时*/
        else {
            price = new BigDecimal(-1);
        }
        return price;
    }
}
